use chia_node::xchs::Xchs;
use serde_json::Value;

use std::fs::File;
use std::io::Read;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 打开文件，这里假设文件名为 "data.json"
    let file_path = "ini.json";
    let mut file = File::open(file_path).expect("Unable to open file");

    // 读取文件内容
    let mut file_content = String::new();
    file.read_to_string(&mut file_content)
        .expect("Unable to read file");

    // 使用 serde_json 解析文件内容
    let parsed_json: Value = serde_json::from_str(&file_content).expect("JSON parsing failed");

    let ssl_file = parsed_json["ssl_file"].as_str().unwrap();
    let wallet_id: usize = parsed_json["wallet_id"].to_string().parse()?;
    let fingerprint = parsed_json["fingerprint"].as_str().unwrap();
    let tick = parsed_json["tick"].as_str().unwrap();
    let time: usize = parsed_json["time"].to_string().parse()?;
    let fee: usize = parsed_json["fee"].to_string().parse()?;
    let wait_time: usize = parsed_json["wait_time"].to_string().parse()?;
    let target_address: &str = parsed_json["target_address"].as_str().unwrap();
    let delay: u64 = parsed_json["delay"].to_string().parse()?;
    let amt: usize = parsed_json["amt"].to_string().parse()?;
    let total_fee = time * fee;
    println!("若您当前钱包的资金足够，将最终消耗{total_fee}mojos的fee，您是否继续？(y/else)");
    let mut y_or_no = String::new();
    std::io::stdin()
        .read_line(&mut y_or_no)
        .expect("无法输入。");
    let y_or_no = y_or_no.trim();
    if y_or_no != "y" {
        return Ok(());
    }
    let mut xchs_client = Xchs::new(
        &ssl_file,
        &fingerprint,
        wallet_id,
        &tick,
        time,
        fee,
        wait_time,
        target_address,
        delay,
        amt,
    )
    .await?;

    Ok(xchs_client.mint().await?)
}
